import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView,
  },
  {
    path:'',
    name:'home',
    component:HomeView,
    redirect:'/welcome',
    children:[
      {
        path: '/welcome',
        name:'welcome',
        component: () => import('../views/WelcomeView.vue')
      }
    ]
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router

  // 做router跳转的login_required验证
  router.beforeEach((to,from,next) => {
    if (to.path =='/login'){
      next()
    }else{
      // 获取token值
      const token = sessionStorage.getItem('token')
      if(!token){
        next('/login')
      }
      next()
    }
  })